﻿using System;
using System.IO;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Speech.Synthesis;
using System.Globalization;
using Renci.SshNet;
using Tellstick;
using System.Reflection;
using System.Timers;
using System.Speech.Recognition;
using System.Windows.Forms;

namespace Alfred
{
    class Alfred
    {
		enum State { Sleep, Awake };

		private SpeechSynthesizer synthesizer;
		private State state { get; set; }
		public RecognitionResult result { private get; set; }
		public string lastCommand { get; set; }

        public Alfred()
        {
			state = State.Sleep;

			synthesizer = new SpeechSynthesizer();
			var voix = synthesizer.GetInstalledVoices(new CultureInfo("fr-FR"));
			synthesizer.SelectVoice(voix[2].VoiceInfo.Name);
        }

        public void Start()
        {
			state = State.Awake;
            synthesizer.Speak("Oui Monsieur !");
        }

        public void StopListening()
        {
			state = State.Sleep;
        }

        public void Stop()
        {
            synthesizer.SpeakAsyncCancelAll();
        }

		public void Encore()
		{
			MethodInfo methodInfo = typeof(Alfred).GetMethod(lastCommand);
			methodInfo.Invoke(this, null);
		}

        public void Time()
        {
            String h = String.Format("{0:00} heure {1:00}", System.DateTime.Now.Hour, System.DateTime.Now.Minute);
            synthesizer.Speak("Il est : " + h);
        }

        public void Search(string s)
        {
            Wiki w = new Wiki();
            w.LoadFromQuery(s);
            Speak(w.title);
            Speak(w.content);
        }

        public void PlayMovie(string movie)
        {
			Film.playMovie(movie);
        }

        public void Speak(string reponse)
        {
            if (reponse != null)
                synthesizer.SpeakAsync(reponse);
        }

		public void VolumeUp()
		{
			for (int i = 0; i < 30; i++)
				SendKeys.Send("{VOLUME_UP}");
		}

		public void VolumeDown()
		{
			for (int i = 0; i < 30; i++)
				SendKeys.Send("{VOLUME_DOWN}");
		}

        public void Allume()
        {
			Device.Light(1, result.Semantics["piece"].Value.ToString());
        }

		public void Eteins()
		{
			Device.Light(0, result.Semantics["piece"].Value.ToString());
		}

		public void TurnUp()
		{
			Device.Light(100, result.Semantics["piece"].Value.ToString());
		}

		public void TurnDown()
		{
			Device.Light(10, result.Semantics["piece"].Value.ToString());
		}

        public void LaunchScenario()
        {
			Scenario scenar = Scenario.getScenarioByName(result.Semantics["mode"].Value.ToString());
            scenar.launchScenario();
        }

        public void MovieList()
        {
            Speak(Allocine.StringMovies(result.Semantics["area"].Value.ToString()));
        }

        public void MovieHour()
        {
			Speak(Allocine.GetMovieInfo(result.Semantics["film"].Value.ToString(), result.Semantics["area"].Value.ToString()).StringSeance("", "VO"));
        }

        public void ShowcaseToday()
        {
			DateTime day = StringUtils.FormatStringDay(result.Semantics["day"].Value.ToString());
            Speak(Showcase.ProgEventToString(Showcase.GetProgDay(day)));
        }

		public void ShowcaseAll()
        {
			DateTime day = StringUtils.FormatStringDay(result.Semantics["day"].Value.ToString());
            Speak(Showcase.ProgListToString(Showcase.GetProg()));
        }

        public void PlaylistArtist()
        {
			string artist = result.Semantics["artist"].Value.ToString();

			Playlist pl = new Playlist();
			pl.createPlaylistByArtist(artist);
			pl.launchPlaylist(artist);
        }

		public void PlaylistGenre()
		{
			string genre = result.Semantics["genre"].Value.ToString();

			Playlist pl = new Playlist();
			pl.createPlaylistByGenre(genre);
			pl.launchPlaylist(genre);
		}
    }
}
